package com.youi.util;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;

/**
 * 请求响应工具
 * @author  erdong.Chan
 *
 */
public class RequestUtil {
	
	public static String POST="POST";
	
	public static String GET="GET";
	
	public static String JSONCONTENETYPE="json;charset=UTF-8";
	
	public static String URLENCODED="x-www-form-urlencoded";
	
	private static String redirectUrl="https://wx.ibranco.com/";   //重定向地址
	
	/**
	 * 请求
	 * @param urlStr
	 * @param method
	 * @param params       参数
	 * @param memo		      备注
	 * @param contentType  文本类型 x-www-form-urlencoded  or  json;charset=UTF-8
	 * @return
	 */
	public static String Action(String urlStr,String method,String params,String memo,String contentType){
		LogUtil.Info("响应url"+urlStr);
		LogUtil.Info("开始"+memo);
		LogUtil.Info("params="+params);
		System.out.println("params="+params);
		try {
			URL url = new URL(urlStr);
			HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
			urlConnection.setDoOutput(true);
			urlConnection.setDoInput(true);
			urlConnection.setRequestMethod(method);
			urlConnection.setUseCaches(false); 
			urlConnection.setInstanceFollowRedirects(true);
			if (contentType!=null) {
				urlConnection.setRequestProperty("Content-Type","application/"+contentType);
			}
			urlConnection.setRequestProperty("Charset", "UTF-8"); 
			urlConnection.connect();
			DataOutputStream out = new DataOutputStream(urlConnection.getOutputStream());
			//参数写入
			if (params!=null&&params.length()>0) {
				out.write(params.getBytes());
			}
	        out.flush();
	        out.close(); 
	        BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"UTF-8"));
	        String line;
	        String responseStr="";
	        while ((line = reader.readLine()) != null){
	        	responseStr+=line;
	        }
	        reader.close();
	        urlConnection.disconnect();
	        LogUtil.Info("postresult="+responseStr);
	        System.out.println("postresult="+responseStr);
	        return responseStr;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			LogUtil.Info(memo+"异常");
			LogUtil.Info(e.toString());
			System.err.println(memo+"异常");
			System.err.println(e.toString());
			return null;
		}
	}
	
	/**
	 * 发送模版消息
	 * @param first
	 * @param keyword1
	 * @param Keyword2
	 * @param Keyword3
	 * @param Keyword4
	 * @param Keyword5
	 * @param remark
	 * @param url
	 * @param unionid
	 * @param type
	 * @param userName
	 */
	private static void ibrancowxTemplateSendFansMsg(String first,String keyword1,String Keyword2,String Keyword3,String Keyword4,String Keyword5,String Keyword6,String Keyword7,String remark,String url,String unionid,String type,String userName){
		String urlStr="https://www.ibranco.com/ibrancowx/template/sendFansMsg.action";
		String params=getParams(first, keyword1, Keyword2, Keyword3, Keyword4, Keyword5, Keyword6, Keyword7, remark, url, unionid, type, userName);
		Action(urlStr, POST, params, "发送模版消息", URLENCODED);
	}
	
	/**
	 * 支付提醒
	 * @param contene
	 * @param payType
	 * @param amount
	 * @param time
	 * @param remark
	 */
	public static void payRemind(String content,String payType,String amount,String time,String remark,String unionid,String userName){
		ibrancowxTemplateSendFansMsg(null, content, payType, amount, time, null,null,null, remark, redirectUrl, unionid, "PAY_REMIND", userName);
	}
	
	/**
	 * 发货提醒
	 * @param goodName
	 * @param goodsDesc
	 * @param time
	 * @param expressCompany
	 * @param expressNumber
	 * @param address
	 * @param unionid
	 * @param userName
	 */
	public static void transport(String goodName,String orderNumber,String expressCompany,String expressNumber,String unionid,String userName){
		ibrancowxTemplateSendFansMsg(goodName, orderNumber, expressCompany, expressNumber, null, null, null, null, null, redirectUrl, unionid, "TRANSPORT", userName);
	}
	
	/**
	 * 积分变动提醒
	 */
	public static void score(String time,String desc,String addOrReduc,String changeScore,String score,String unionid,String userName){
		ibrancowxTemplateSendFansMsg(null, null, time, desc, addOrReduc, changeScore, null, score, null, redirectUrl, unionid, "INTEGRAL_REMIND", userName);
	}
	
	/**
	 * 获取参数
	 * @param first
	 * @param keyword1
	 * @param Keyword2
	 * @param Keyword3
	 * @param Keyword4
	 * @param Keyword5
	 * @param remark
	 * @param url
	 * @param unionid
	 * @param type
	 * @param userName
	 * @return
	 */
	private static String getParams(String first,String keyword1,String Keyword2,String Keyword3,String Keyword4,String Keyword5,String Keyword6,String Keyword7,String remark,String url,String unionid,String type,String userName){
		String params="";
		if (first!=null&&first.length()>0) {
			params+="first="+first+"&";
		}
		if (keyword1!=null&&keyword1.length()>0) {
			params+="keyword1="+keyword1+"&";
		}
		if (Keyword2!=null&&Keyword2.length()>0) {
			params+="Keyword2="+Keyword2+"&";
		}
		if (Keyword3!=null&&Keyword3.length()>0) {
			params+="Keyword3="+Keyword3+"&";
		}
		if (Keyword4!=null&&Keyword4.length()>0) {
			params+="Keyword4="+Keyword4+"&";
		}
		if (Keyword5!=null&&Keyword5.length()>0) {
			params+="Keyword5="+Keyword5+"&";
		}
		if (Keyword6!=null&&Keyword6.length()>0) {
			params+="Keyword6="+Keyword6+"&";
		}
		if (Keyword7!=null&&Keyword7.length()>0) {
			params+="Keyword7="+Keyword7+"&";
		}
		if (remark!=null&&remark.length()>0) {
			params+="remark="+remark+"&";
		}
		if (url!=null&&url.length()>0) {
			params+="url="+url+"&";
		}
		if (unionid!=null&&unionid.length()>0) {
			params+="unionid="+unionid+"&";
		}
		if (type!=null&&type.length()>0) {
			params+="type="+type+"&";
		}
		if (userName!=null&&userName.length()>0) {
			params+="userName="+userName+"&";
		}
		if (params.length()>0) {
			params=params.substring(0, params.length()-1);
		}
		return params;
	}
	
	public static void main(String[] args) {
		String unionid="oW_tUxONzEqHlkJoSsfq_2f7d3m8";
		payRemind("keyword1", "云支付", "100", DateUtil.simpdfyMdHms.format(new Date()), "我是备注", unionid, "耳东先生");
		//transport("我是商品名称", "我是商品描述", DateUtil.simpdfyMdHms.format(new Date()), "顺丰", "10086", "广州大学城", unionid, "耳东先生");
	}
}
